//
//  ModifySignViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/11.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "ModifySignViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "MeViewModel.h"

@interface ModifySignViewController ()
@property (nonatomic ,weak) IBOutlet UIView *navView;
@property (nonatomic ,weak) IBOutlet UITextView *textView;
@property (nonatomic ,weak) IBOutlet UIButton *saveButton;


- (IBAction)dismissSelf:(id)sender;
@end

@implementation ModifySignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _navView.layer.shadowColor = [[UIColor colorWithRed:224.0/255 green:79.0/255 blue:95.0/255 alpha:1.0] CGColor];
    _navView.layer.shadowOffset = CGSizeMake(1.0, 1.0);
    _navView.layer.shadowOpacity = .3;
    
    NSString *sign = [[NSUserDefaults standardUserDefaults] objectForKey:@"sign"];
    
    _textView.text = [sign length]>0?sign:@"输入自己的个性签名";
    
    RAC(_saveButton,enabled) = [RACSignal combineLatest:@[_textView.rac_textSignal] reduce:^id(NSString *string){
        return @(!([string isEqualToString:@"输入自己的个性签名"] || [string length]==0) );
    }];
    
    RAC(_saveButton,titleLabel.textColor) = [RACSignal combineLatest:@[_textView.rac_textSignal] reduce:^id(NSString *string){
        BOOL enabled = ! ([string isEqualToString:@"输入自己的个性签名"] || [string length]==0);
        
        return enabled?[UIColor redColor]:[UIColor lightGrayColor];
        
    }];
    
    @weakify(self);
    [[_saveButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
    
        [[MeViewModel setSign:_textView.text] subscribeNext:^(id x) {
            @strongify(self);
            
            [[NSUserDefaults standardUserDefaults] setObject:_textView.text forKey:@"sign"];
            
            
            [self.delegate singViewController:self hasFinishedUpdate:YES];
        
        } error:^(NSError *error) {
            
        }];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dismissSelf:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
